package com.example.distinguishcolors.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.distinguishcolors.R;

/**
 * @author Yingyong Lao
 * 创建时间 2021/10/2 14:49
 * @version 1.0
 */
public class LyyDialog extends Dialog {
    private View dialogView;
    private TextView titleTv;//标题
    private TextView contentTv;//内容
    private TextView confirmTv;//确认
    private TextView cancelTv;//左侧的取消按钮
    private EditText nameEt;//名称输入框
    private View middleVerticalLine;//
    private OnBottomTvClickCallBack callBack;
    @SuppressLint("StaticFieldLeak")
    private static LyyDialog dialog;
    public LyyDialog(@NonNull Context context,OnBottomTvClickCallBack callBack) {
        super(context, R.style.LyyDialogStyle);
        dialog=this;
        dialogView= LayoutInflater.from(context).inflate(R.layout.lyy_dialog_layout,null);
        titleTv=dialogView.findViewById(R.id.titleTv);
        contentTv=dialogView.findViewById(R.id.contentTv);
        confirmTv=dialogView.findViewById(R.id.confirmTv);
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(callBack!=null){
                    callBack.onPositiveClick();
                }
                dismiss();
            }
        });
        middleVerticalLine=dialogView.findViewById(R.id.middleVerticalLine);
        cancelTv=dialogView.findViewById(R.id.cancelTv);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(callBack!=null){
                    callBack.onNegativeClick();
                }
            }
        });
        nameEt=dialogView.findViewById(R.id.nameEt);
        this.callBack=callBack;
        setCancelable(false);
    }

    public LyyDialog(@NonNull Context context,OnBottomTvClickCallBack callBack,boolean isShowNegativeTv) {
        this(context,callBack);//调用其他构造器
        if(isShowNegativeTv){
            cancelTv.setVisibility(View.VISIBLE);
            middleVerticalLine.setVisibility(View.VISIBLE);
        }
    }

    public LyyDialog(Context context,OnBottomTvClickCallBack callBack,boolean isShowNegativeTv,boolean isShowNameEt){
        this(context,callBack,isShowNegativeTv);
        if(isShowNameEt){
            nameEt.setVisibility(View.VISIBLE);
        }
    }

    public void setTitle(String title){
        titleTv.setText(TextUtils.isEmpty(title)?"":title);
    }

    public void setContent(String content){
        contentTv.setText(TextUtils.isEmpty(content)?"":content);
    }

    public EditText getNameEt() {
        return nameEt;
    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setContentView(dialogView);
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        super.show();
    }

    public static interface OnBottomTvClickCallBack{
        public void onPositiveClick();
        public void onNegativeClick();
    }
    public abstract static class SelectCallBack implements OnBottomTvClickCallBack {
        @Override
        public void onPositiveClick() {

        }

        @Override
        public void onNegativeClick() {
            dismiss();

        }

        public void dismiss(){
            dialog.dismiss();
        }

        public EditText getNameEt(){
            return dialog.getNameEt();
        }
    }
}
